/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdministrarElector.java
 *
 * Created on 14-ene-2013, 7:00:02
 */
package sisvoto.vista;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.jdesktop.beansbinding.BindingGroup;
import sisvoto.DAO.ConsultasBD;
import sisvoto.ModeloDAO.ElectorDAO;
import sisvoto.Utiles.Mensajes;
import sisvoto.Utiles.Util;
import sisvoto.modelo.Anio_Paralelo;
import sisvoto.modelo.Elector;
import sisvoto.modelo.Paralelo;
import sisvoto.modeloTablas.ModelTableElector;

/**
 *
 * @author DJ2-PC
 */
public class AdministrarElector extends javax.swing.JDialog {

    Elector elect;
    ArrayList<Elector> electores;
    ArrayList<Elector> electoresbinding;
    BindingGroup bind= null;
    private Elector elector;
    private List<Anio_Paralelo> anioparalelos;
    private Anio_Paralelo anioparalelo;
    //private List<Elector> electores;
    private int variable;

    /** Creates new form AdministrarElector */
    public AdministrarElector(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.anioparalelos = (ArrayList<Anio_Paralelo>) new ConsultasBD().buscarTodos("Anio_Paralelo");
        this.elector = new Elector();
        //this.electores = new ArrayList<>();
        this.initComponents();
        consultarElectores();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jButton1 = new javax.swing.JButton();
        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLblAdmElector = new javax.swing.JLabel();
        jLblAEduBas = new javax.swing.JLabel();
        jCbxAEduBas = new javax.swing.JComboBox();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTblAdmElector = new javax.swing.JTable();
        jBtnNuevoElector = new javax.swing.JButton();
        jBtnAgregarHuellaElector = new javax.swing.JButton();
        jBtnEditarElector = new javax.swing.JButton();
        jBtnEliminarElector = new javax.swing.JButton();
        jBtnGuardarElector = new javax.swing.JButton();
        jLblNombreElector = new javax.swing.JLabel();
        jLblApellidosElector = new javax.swing.JLabel();
        jLblCedulaElector = new javax.swing.JLabel();
        jTxtNombreElector = new javax.swing.JTextField();
        jTxtApellidosElector = new javax.swing.JTextField();
        jTxtCedulaElector = new javax.swing.JTextField();
        jLblApellidosElector1 = new javax.swing.JLabel();
        jtxtIdentidad = new javax.swing.JTextField();
        jLblApellidosElector2 = new javax.swing.JLabel();
        jdtFechaNacimiento = new com.toedter.calendar.JDateChooser();
        jLblApellidosElector3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtaDireccion = new javax.swing.JTextArea();
        jBtnAgregarHuellaElector1 = new javax.swing.JButton();
        btnbuscar = new javax.swing.JButton();
        jLblCedulaElector1 = new javax.swing.JLabel();
        jcbCedula = new javax.swing.JComboBox();
        txtBuscar = new javax.swing.JTextField();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Electores");

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/''_10102012_1738.jpeg"))); // NOI18N
        imagePanel1.setLayout(null);

        curvesPanel1.setLayout(null);

        jLblAdmElector.setFont(new java.awt.Font("Georgia", 3, 18));
        jLblAdmElector.setForeground(new java.awt.Color(255, 255, 255));
        jLblAdmElector.setText("Gestión de Electores");
        curvesPanel1.add(jLblAdmElector);
        jLblAdmElector.setBounds(160, 20, 320, 21);

        jLblAEduBas.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblAEduBas.setForeground(new java.awt.Color(255, 255, 255));
        jLblAEduBas.setText("Año de Educación:");
        curvesPanel1.add(jLblAEduBas);
        jLblAEduBas.setBounds(40, 320, 140, 30);

        jCbxAEduBas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${anioparalelos}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jCbxAEduBas);
        bindingGroup.addBinding(jComboBoxBinding);

        jCbxAEduBas.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCbxAEduBasItemStateChanged(evt);
            }
        });
        curvesPanel1.add(jCbxAEduBas);
        jCbxAEduBas.setBounds(180, 320, 200, 30);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Elector", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 10), java.awt.Color.white)); // NOI18N
        jPanel1.setForeground(new java.awt.Color(255, 255, 255));
        jPanel1.setOpaque(false);
        jPanel1.setLayout(null);

        jTblAdmElector.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Cédula", "Nombres y Apellidos"
            }
        ));
        jTblAdmElector.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTblAdmElectorMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTblAdmElector);

        jPanel1.add(jScrollPane1);
        jScrollPane1.setBounds(10, 20, 470, 160);

        curvesPanel1.add(jPanel1);
        jPanel1.setBounds(20, 110, 490, 190);

        jBtnNuevoElector.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/document_32.png"))); // NOI18N
        jBtnNuevoElector.setText("Nuevo");
        jBtnNuevoElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnNuevoElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnNuevoElector);
        jBtnNuevoElector.setBounds(390, 320, 120, 41);

        jBtnAgregarHuellaElector.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/huella.gif"))); // NOI18N
        jBtnAgregarHuellaElector.setText("Huella");
        jBtnAgregarHuellaElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAgregarHuellaElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnAgregarHuellaElector);
        jBtnAgregarHuellaElector.setBounds(390, 520, 120, 40);

        jBtnEditarElector.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/Pencil3.png"))); // NOI18N
        jBtnEditarElector.setText("Editar");
        jBtnEditarElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEditarElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnEditarElector);
        jBtnEditarElector.setBounds(390, 370, 120, 40);

        jBtnEliminarElector.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/trash_32.png"))); // NOI18N
        jBtnEliminarElector.setText("Eliminar");
        jBtnEliminarElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEliminarElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnEliminarElector);
        jBtnEliminarElector.setBounds(390, 420, 120, 41);

        jBtnGuardarElector.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/save_32.png"))); // NOI18N
        jBtnGuardarElector.setText("Guardar");
        jBtnGuardarElector.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnGuardarElectorActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnGuardarElector);
        jBtnGuardarElector.setBounds(390, 470, 120, 40);

        jLblNombreElector.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblNombreElector.setForeground(new java.awt.Color(255, 255, 255));
        jLblNombreElector.setText("Nombres:");
        curvesPanel1.add(jLblNombreElector);
        jLblNombreElector.setBounds(40, 440, 68, 30);

        jLblApellidosElector.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblApellidosElector.setForeground(new java.awt.Color(255, 255, 255));
        jLblApellidosElector.setText("Dirección:");
        curvesPanel1.add(jLblApellidosElector);
        jLblApellidosElector.setBounds(40, 560, 90, 30);

        jLblCedulaElector.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblCedulaElector.setForeground(new java.awt.Color(255, 255, 255));
        jLblCedulaElector.setText("Buscar por:");
        curvesPanel1.add(jLblCedulaElector);
        jLblCedulaElector.setBounds(30, 70, 100, 30);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elector.nombre}"), jTxtNombreElector, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        curvesPanel1.add(jTxtNombreElector);
        jTxtNombreElector.setBounds(120, 440, 260, 30);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elector.apellido}"), jTxtApellidosElector, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        curvesPanel1.add(jTxtApellidosElector);
        jTxtApellidosElector.setBounds(120, 400, 260, 30);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elector.cedula}"), jTxtCedulaElector, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        curvesPanel1.add(jTxtCedulaElector);
        jTxtCedulaElector.setBounds(120, 360, 260, 30);

        jLblApellidosElector1.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblApellidosElector1.setForeground(new java.awt.Color(255, 255, 255));
        jLblApellidosElector1.setText("Apellidos:");
        curvesPanel1.add(jLblApellidosElector1);
        jLblApellidosElector1.setBounds(40, 400, 80, 30);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elector.identidadDactilar}"), jtxtIdentidad, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        curvesPanel1.add(jtxtIdentidad);
        jtxtIdentidad.setBounds(200, 480, 180, 30);

        jLblApellidosElector2.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblApellidosElector2.setForeground(new java.awt.Color(255, 255, 255));
        jLblApellidosElector2.setText("Identidad Dactilar:");
        curvesPanel1.add(jLblApellidosElector2);
        jLblApellidosElector2.setBounds(40, 480, 160, 30);
        curvesPanel1.add(jdtFechaNacimiento);
        jdtFechaNacimiento.setBounds(200, 520, 180, 30);

        jLblApellidosElector3.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblApellidosElector3.setForeground(new java.awt.Color(255, 255, 255));
        jLblApellidosElector3.setText("Fecha Nacimiento:");
        curvesPanel1.add(jLblApellidosElector3);
        jLblApellidosElector3.setBounds(40, 520, 160, 30);

        jtaDireccion.setColumns(20);
        jtaDireccion.setRows(5);

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${elector.direccion}"), jtaDireccion, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jScrollPane2.setViewportView(jtaDireccion);

        curvesPanel1.add(jScrollPane2);
        jScrollPane2.setBounds(120, 560, 260, 50);

        jBtnAgregarHuellaElector1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/DeleteRed.png"))); // NOI18N
        jBtnAgregarHuellaElector1.setText("Salir");
        jBtnAgregarHuellaElector1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnAgregarHuellaElector1ActionPerformed(evt);
            }
        });
        curvesPanel1.add(jBtnAgregarHuellaElector1);
        jBtnAgregarHuellaElector1.setBounds(390, 570, 120, 40);

        btnbuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/sisvoto/Recursos/search_32.png"))); // NOI18N
        btnbuscar.setToolTipText("Lista de Electores");
        btnbuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnbuscarActionPerformed(evt);
            }
        });
        curvesPanel1.add(btnbuscar);
        btnbuscar.setBounds(460, 70, 40, 30);

        jLblCedulaElector1.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblCedulaElector1.setForeground(new java.awt.Color(255, 255, 255));
        jLblCedulaElector1.setText("Cédula:");
        curvesPanel1.add(jLblCedulaElector1);
        jLblCedulaElector1.setBounds(40, 360, 70, 30);

        jcbCedula.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cédula", "Apellidos", "Nombres", "Todos" }));
        jcbCedula.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcbCedulaItemStateChanged(evt);
            }
        });
        curvesPanel1.add(jcbCedula);
        jcbCedula.setBounds(120, 70, 100, 30);

        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBuscarKeyPressed(evt);
            }
        });
        curvesPanel1.add(txtBuscar);
        txtBuscar.setBounds(230, 70, 220, 30);

        imagePanel1.add(curvesPanel1);
        curvesPanel1.setBounds(0, 0, 540, 630);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 541, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jCbxAEduBasItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCbxAEduBasItemStateChanged
// TODO add your handling code here:

    if (variable == 2) {
        this.anioparalelo = anioparalelos.get(jCbxAEduBas.getSelectedIndex());
        this.electores = (ArrayList<Elector>) new ConsultasBD().buscarPOR_ORDER("Elector", "anioparalelo", "'" + this.anioparalelo.getId() + "'", "apellido");
        actualizartabla();
    }


}//GEN-LAST:event_jCbxAEduBasItemStateChanged

private void jBtnAgregarHuellaElector1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAgregarHuellaElector1ActionPerformed
// TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jBtnAgregarHuellaElector1ActionPerformed

private void jBtnNuevoElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnNuevoElectorActionPerformed
// TODO add your handling code here:

    this.anioparalelos = (ArrayList<Anio_Paralelo>) new ConsultasBD().buscarTodos("Anio_Paralelo");
    ActivarCasillas();
    consultarElectores();
    actualizartabla();
    limpiarCampos();


}//GEN-LAST:event_jBtnNuevoElectorActionPerformed

private void jBtnEditarElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEditarElectorActionPerformed
// TODO add your handling code here:
    if (jTblAdmElector.getSelectedRow() != -1) {
        if (elector.getId() != null) {
            ActivarCasillas();
        }
    } else {
        new Mensajes().StandarWarning(this, "Seleccione el Elector que Desea Editar");
    }
}//GEN-LAST:event_jBtnEditarElectorActionPerformed

private void jBtnEliminarElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEliminarElectorActionPerformed
// TODO add your handling code here:
    if (jTblAdmElector.getSelectedRow() != -1) {
        String idpriv = String.valueOf(jTblAdmElector.getValueAt(jTblAdmElector.getSelectedRow(), 0));
        ArrayList<Elector> paux = (ArrayList<Elector>) new ConsultasBD().buscarPOR("Elector", "cedula", "'" + idpriv + "'");
        this.elector = paux.get(0);
        if (new Mensajes().ConfirmacionDialogowarning(this, "¿Está seguro de Eliminar éste Registro?") == 0) {
            try {
                ElectorDAO eDAO = new ElectorDAO(elector);
                eDAO.update();
                eDAO.remove();
                new Mensajes().ConfirmacionDialogotick(this, "El Registro se ha Eliminado Con Éxito");
                ActivarCasillas();
                consultarElectores();
                actualizartabla();
                limpiarCampos();
            } catch (Exception ex) {
                // ex.printStackTrace();
                new Mensajes().StandarWarning(this, "                 No es Posible Eliminar. \n Éste Registro está Siendo Utilizado por el Sistema");
            }
        }
    } else {
        new Mensajes().StandarWarning(this, "Seleccione el Elector que Desea Eliminar");
    }
}//GEN-LAST:event_jBtnEliminarElectorActionPerformed

private void jBtnGuardarElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnGuardarElectorActionPerformed
// TODO add your handling code here:
    if(jCbxAEduBas.getItemCount()==0){
        new Mensajes().StandarWarning(this, "Aun no ha Gestionado Años de Educación");
        return;
    }
    Util u = new Util();
    if (ValidarCampos()) {
        if (u.esCedulaValida(jTxtCedulaElector.getText())) {
            ElectorDAO edao;
            if (this.elector.getId() == null) {
                if (existeElectorNuevo()) {
                    elector.setAnioparalelo(anioparalelo);
                    elector.setFecha_Nacimiento(jdtFechaNacimiento.getDate());
                    edao = new ElectorDAO(elector);
                    try {
                        edao.persist();
                        new Mensajes().StandarTick(this, "Elector Registrado con Éxito");
                        ActivarCasillas();
                        consultarElectores();
                        actualizartabla();
                        limpiarCampos();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } else {
                    new Mensajes().StandarWarning(this, "    Ya Existe un Elector Registrado \n Con el Número de Cédula Ingresado");
                }
            } else {
                if (existeElectorActualizar()) {
                    elector.setAnioparalelo(anioparalelo);
                    elector.setFecha_Nacimiento(jdtFechaNacimiento.getDate());
                    edao = new ElectorDAO(elector);
                    try {
                        edao.update();
                        new Mensajes().StandarTick(this, "Elector Actualizado con Éxito");
                        ActivarCasillas();
                        consultarElectores();
                        actualizartabla();
                        limpiarCampos();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                } else {
                    new Mensajes().StandarWarning(this, "             No es posible Guardar. \n Otro Elector ya tiene Registrado la cédula Ingresada");
                }
            }
        } else {
            new Mensajes().StandarWarning(this, "La Cédula Ingresada No es Correcta");
        }
    } else {
        new Mensajes().StandarWarning(this, "Ingrese Todos los Campos Obligatorios");
    }
}//GEN-LAST:event_jBtnGuardarElectorActionPerformed

private void btnbuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnbuscarActionPerformed
// TODO add your handling code here:
    if(txtBuscar.getText().isEmpty()|| jcbCedula.getSelectedItem().toString().equals("Todos")){
        this.electores = (ArrayList<Elector>) new ConsultasBD().buscarTodos("Elector");
        actualizartabla();
    }else{
        
        if(jcbCedula.getSelectedItem().toString().equals("Cédula")){
            if(new Util().esCedulaValida(txtBuscar.getText())){
                this.electores = (ArrayList<Elector>) new ConsultasBD().buscarPOR("Elector", "cedula", "'" + txtBuscar.getText() + "'");
            }else{
                new Mensajes().StandarWarning(this,"Ingrese una Cédula Correcta");
            }       
        }else{
            if(jcbCedula.getSelectedItem().toString().equals("Apellidos")){
                this.electores = (ArrayList<Elector>) new ConsultasBD().buscarLIKE("Elector", "apellido", txtBuscar.getText());
            }
            if(jcbCedula.getSelectedItem().toString().equals("Nombres")){
                this.electores = (ArrayList<Elector>) new ConsultasBD().buscarLIKE("Elector", "nombre", txtBuscar.getText() );
            }
        }
        actualizartabla();
    }
   
}//GEN-LAST:event_btnbuscarActionPerformed

private void jTblAdmElectorMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTblAdmElectorMouseClicked
// TODO add your handling code here:
    if (jTblAdmElector.getSelectedRow() != -1) {
        String idpriv = (String) jTblAdmElector.getValueAt(jTblAdmElector.getSelectedRow(), 0);
        ArrayList<Elector> paux = (ArrayList<Elector>) new ConsultasBD().buscarPOR("Elector", "cedula", "'" + idpriv + "'");
        this.elector = paux.get(0);
        cargarCampos(elector);
        desActivarCasillas();
    }

}//GEN-LAST:event_jTblAdmElectorMouseClicked

private void jcbCedulaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcbCedulaItemStateChanged
// TODO add your handling code here:
    txtBuscar.setText("");
}//GEN-LAST:event_jcbCedulaItemStateChanged

private void txtBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyPressed
// TODO add your handling code here:
     if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        btnbuscar.requestFocus();
        btnbuscarActionPerformed(null);
    }
}//GEN-LAST:event_txtBuscarKeyPressed

private void jBtnAgregarHuellaElectorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnAgregarHuellaElectorActionPerformed
// TODO add your handling code here:
    if(elector.getId()!=null){
        RegistrarHuella h = new RegistrarHuella(null, true, elector);
        h.setLocationRelativeTo(this);
        h.setVisible(true);
    }else{
       new Mensajes().StandarWarning(this, "Seleccione el Elector");
    }
}//GEN-LAST:event_jBtnAgregarHuellaElectorActionPerformed

    /**
     * @param args the command line arguments
     */
 

    public Elector getElector() {
        return elector;
    }

    public void setElector(Elector elector) {
        this.elector = elector;
    }

    public List<Anio_Paralelo> getAnioparalelos() {
        return anioparalelos;
    }

    public void setAnioparalelos(List<Anio_Paralelo> anioparalelos) {
        this.anioparalelos = anioparalelos;
    }

    public ArrayList<Elector> getElectores() {
        return electores;
    }

    public void setElectores(ArrayList<Elector> electores) {
        this.electores = electores;
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnbuscar;
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private javax.swing.JButton jBtnAgregarHuellaElector;
    private javax.swing.JButton jBtnAgregarHuellaElector1;
    private javax.swing.JButton jBtnEditarElector;
    private javax.swing.JButton jBtnEliminarElector;
    private javax.swing.JButton jBtnGuardarElector;
    private javax.swing.JButton jBtnNuevoElector;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jCbxAEduBas;
    private javax.swing.JLabel jLblAEduBas;
    private javax.swing.JLabel jLblAdmElector;
    private javax.swing.JLabel jLblApellidosElector;
    private javax.swing.JLabel jLblApellidosElector1;
    private javax.swing.JLabel jLblApellidosElector2;
    private javax.swing.JLabel jLblApellidosElector3;
    private javax.swing.JLabel jLblCedulaElector;
    private javax.swing.JLabel jLblCedulaElector1;
    private javax.swing.JLabel jLblNombreElector;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTblAdmElector;
    private javax.swing.JTextField jTxtApellidosElector;
    private javax.swing.JTextField jTxtCedulaElector;
    private javax.swing.JTextField jTxtNombreElector;
    private javax.swing.JComboBox jcbCedula;
    private com.toedter.calendar.JDateChooser jdtFechaNacimiento;
    private javax.swing.JTextArea jtaDireccion;
    private javax.swing.JTextField jtxtIdentidad;
    private javax.swing.JTextField txtBuscar;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private void consultarElectores() {
        if (this.anioparalelos.isEmpty()) {
            new Mensajes().StandarWarning(this, "No existen Años de Educación Agregados");
        } else {
            //bindingGroup.unbind();
            this.anioparalelo = anioparalelos.get(0);
            jCbxAEduBas.setSelectedItem(anioparalelo);
            this.electores = (ArrayList<Elector>) new ConsultasBD().buscarPOR_ORDER("Elector", "anioparalelo", "'" + this.anioparalelo.getId() + "'", "apellido");
            actualizartabla();
            //bindingGroup.bind();
            variable = 2;
        }
    }

    private void actualizartabla() {
        ModelTableElector modele = new ModelTableElector(this.electores);
        jTblAdmElector.setModel(modele);
        jTblAdmElector.updateUI();
        modele.tamanioColumnas(jTblAdmElector);
    }

    private void limpiarCampos() {
        this.elector = new Elector();
        jTxtCedulaElector.setText("");
        jTxtNombreElector.setText("");
        jTxtApellidosElector.setText("");
        jtxtIdentidad.setText("");
        jdtFechaNacimiento.setDate(new Date());
        jtaDireccion.setText("");

    }

    private void cargarCampos(Elector e) {
        jTxtCedulaElector.setText(e.getCedula());
        jTxtNombreElector.setText(e.getNombre());
        jTxtApellidosElector.setText(e.getApellido());
        jtxtIdentidad.setText(e.getIdentidadDactilar());
        jdtFechaNacimiento.setDate((Date)e.getFecha_Nacimiento());
        jtaDireccion.setText(e.getDireccion());
        jCbxAEduBas.setSelectedItem(e.getAnioparalelo());
    }

    

    private boolean ValidarCampos() {
        if (!jTxtCedulaElector.getText().isEmpty()) {
            if (!jTxtNombreElector.getText().isEmpty()) {
                if (!jTxtApellidosElector.getText().isEmpty()) {
                    if (!jtxtIdentidad.getText().isEmpty()) {
                        return true;
                    }
                }
            }

        }
        return false;
    }

    private boolean existeElectorNuevo() {
        //retorna true si el usuario ya existe
        ArrayList<Elector> usuars = (ArrayList<Elector>) new ConsultasBD().buscarPOR("Elector", "cedula", "'" + jTxtCedulaElector.getText() + "'");
        if (usuars.isEmpty()) {
            return true;
        }
        return false;
    }
    private boolean existeElectorActualizar() {
        //retorna true si el usuario ya existe

        ArrayList<Elector> usuars = (ArrayList<Elector>) new ConsultasBD().buscarExcepto("Elector", "id", String.valueOf(elector.getId()), "cedula", "'" + elector.getCedula() + "'");
        if (usuars.isEmpty()) {
            return true;
        }
        return false;
    }

    private void desActivarCasillas() {
        jTxtApellidosElector.setEnabled(false);
        jTxtCedulaElector.setEnabled(false);
        jTxtNombreElector.setEnabled(false);
        jtxtIdentidad.setEnabled(false);
        jdtFechaNacimiento.setEnabled(false);
        jtaDireccion.setEnabled(false);

    }

    private void ActivarCasillas() {
        jTxtApellidosElector.setEnabled(true);
        jTxtCedulaElector.setEnabled(true);
        jTxtNombreElector.setEnabled(true);
        jtxtIdentidad.setEnabled(true);
        jdtFechaNacimiento.setEnabled(true);
        jtaDireccion.setEnabled(true);
    }
}
